﻿using System;
using System.Collections.Generic;

namespace Pfz.WpfControls
{
	/// <summary>
	/// Class responsible for setting the Searching function to the actual thread only.
	/// When disposed, it restores the last one.
	/// </summary>
	public sealed class SearcherForThisThread:
		IDisposable
	{
		private Func<SearcherArgs, ICollection<object>> _oldThreadSearching;

		/// <summary>
		/// Instantiate this class if you want to set an specific searching function for the actual thread.
		/// If you want to set one to the entire application, use the Searcher.Searching static property instead.
		/// </summary>
		public SearcherForThisThread(Func<SearcherArgs, ICollection<object>> searching)
		{
			_oldThreadSearching = Searcher._threadSearching;
			Searcher._threadSearching = searching;
		}

		/// <summary>
		/// Recovers the last Searching function for this thread.
		/// </summary>
		public void Dispose()
		{
			Searcher._threadSearching = _oldThreadSearching;
		}

	}
}
